<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <!-- 柱状图 统计物品在库内的数量-->
      <div class="card-panel" @click="handleSetBarItem()">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="nested" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            库存量统计
          </div>
          <!-- <count-to :start-val="0" :end-val="102400" :duration="2600" class="card-panel-num" /> -->
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <!-- 饼状图 统计不同类型所占比例-->
      <div class="card-panel" @click="handleSetPieItemType()">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="nested" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            类型统计
          </div>
          <!-- <count-to :start-val="0" :end-val="81212" :duration="3000" class="card-panel-num" /> -->
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <!-- 折线图 统计报修报废物品数量-->
      <div class="card-panel" @click="handleSetLineOperate()">
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="nested" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            损耗统计
          </div>
          <!-- <count-to :start-val="0" :end-val="9280" :duration="3200" class="card-panel-num" /> -->
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <!-- 柱状图 统计库内物品在一定时间内使用的评数-->
      <div class="card-panel" @click="handleSetBarOutWarehouse()">
        <div class="card-panel-icon-wrapper icon-shopping">
          <svg-icon icon-class="nested" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            使用频率统计
          </div>
          <!-- <count-to :start-val="0" :end-val="13600" :duration="3600" class="card-panel-num" /> -->
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import { listItem} from "@/api/warehousemanager/item";
import { listOutwarehouse} from "@/api/warehousemanager/outwarehouse";
import { listOperate } from "@/api/warehousemanager/operate";

export default {
  components: {
    CountTo
  },
  data(){
    return{
      // 库存统计属性
      itemParams: {
        warehouseId:this.$store.getters.warehouseId,
      },
      itemList:null,
      //损耗统计查询参数
      operateParams: {
        item:{
          itemName:null,
          warehouseId:this.$store.getters.warehouseId,
        },
        operateType: null,
      },
      operateList:null,
      //使用频率属性
      outParams: {
        toWarehouseId: this.$store.getters.warehouseId,
      },
      outWarehouseList:null,
    }
  },
 created(){
    this.handleSetLineOperate();
    this.handleSetBarItem();
    this.handleSetPieItemType();
    this.handleSetBarOutWarehouse();
  },
  emits: ['handleSetLineOperate','handleSetBarItem','handleSetPieItemType','handleSetBarOutWarehouse'],
  methods: {

    //统计库存数量
    handleSetBarItem() {
      listItem(this.itemParams).then(response => {
        this.itemList = response.rows
        const source = []
        this.itemList.forEach(element => {
          const temp = {
             itemName:element.itemName,
             itemNum:element.itemNum
          }
          source.push(temp);
        });
        const dataset = {
          dimensions: ['itemName','itemNum'],
          source:source
        }
        this.$emit('handleSetBarItem',dataset)
      });

    },
    //类型统计
    handleSetPieItemType() {
      listItem(this.itemParams).then(response => {
        this.itemList = response.rows
        const data = []
        const typeNameList = []
        this.itemList.forEach(element => {
          if ( typeNameList.includes(element.itemType.typeName)){
            var res= data.findIndex(item =>{
              return item.name == element.itemType.typeName
            })
            data[res].value =data[res].value + element.itemNum
          }else{
            const temp = {
              value:element.itemNum,
              name:element.itemType.typeName
            }
            data.push(temp);
            typeNameList.push(element.itemType.typeName)
          }
        });
        this.$emit('handleSetPieItemType',data)
      });     
    },
    //损耗统计
    handleSetLineOperate() {
      listOperate(this.operateParams).then(response => {
        this.operateList = response.rows;
        const source = []
        const itemNameList = []
        this.operateList.forEach(element => {
          if ( itemNameList.includes(element.item.itemName)){
            
            var res= source.findIndex(item =>{
              return item.itemName == element.item.itemName
            })
            source[res].itemNum = source[res].itemNum+element.itemNum
          }else{
            const temp = {
              itemName:element.item.itemName,
              itemNum:element.itemNum
            }
            source.push(temp);
            itemNameList.push(element.item.itemName)
          }
        });
        const dataset = {
          dimensions: ['itemName','itemNum'],
          source:source
        }  
        this.$emit('handleSetLineOperate', dataset)
      });

    },
    //使用频率统计
    handleSetBarOutWarehouse(){
      listOutwarehouse(this.outParams).then(response => {
        this.outwarehouseList = response.rows;
        const source = []
        const itemNameList = []
        this.outwarehouseList.forEach(element => {
          if ( itemNameList.includes(element.item.itemName)){
            var res= source.findIndex(item =>{
              return item.itemName == element.item.itemName
            })
            source[res].itemNum = source[res].itemNum+element.itemNum
          }else{
            const temp = {
              itemName:element.item.itemName,
              itemNum:element.itemNum
            }
            source.push(temp);
            itemNameList.push(element.item.itemName)
          }
        });
        const dataset = {
          dimensions: ['itemName','itemNum'],
          source:source
        }
        this.$emit('handleSetBarOutWarehouse',dataset)        
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
